package com.xjzd.pattern.strategy.pay;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PayChannelStrategyService {

    private final Map<String, PaymentService> strategyMap = new HashMap<>();

    public PayChannelStrategyService(List<PaymentService> paymentServices) {
        for (PaymentService paymentService : paymentServices) {
            strategyMap.put(paymentService.getSupportedChannel(), paymentService);
        }
    }

    public void pay(String channel, BigDecimal amount) {
        PaymentService paymentService = strategyMap.get(channel);
        if (null == paymentService) {
            throw new IllegalArgumentException("Unsupported payment channel: " + channel);
        }
        paymentService.pay(amount);
    }
}
